package cn.zxy.controller;

import cn.zxy.exception.BizException;
import cn.zxy.model.Product;
import cn.zxy.model.ResponseDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/api/product")
public class ProductController {


    @GetMapping("/string")
    public String select2(){
        return "今天下雨了，看起来心情不错";
    }

    @GetMapping
    public ResponseDTO select(@RequestParam(defaultValue = "")String name) {
        Product product = Product.builder().id(185).name("西瓜2斤")
                .price(BigDecimal.valueOf(1.08)).qty(10).build();
//        ResponseDTO dto = new ResponseDTO();
//        dto.setCode(0);
//        dto.setData(product);

//        String name = null;
        if (name.equals("自律")){
            throw new BizException(809,"做运动");
        }

//        return dto;
        return ResponseDTO.success(product);
    }

//    @GetMapping
//    public ResponseDTO select(){
//        Product product = Product.builder().id(185).name("西瓜2斤")
//                .price(BigDecimal.valueOf(1.08)).qty(10).build();
//
//        return ResponseDTO.success(product);
//    }

//    @GetMapping
//    public Product select(){
//        Product product = Product.builder().id(185).name("西瓜2斤")
//                .price(BigDecimal.valueOf(1.08)).qty(10).build();
//
//        int i = 5/0;
////        if (true) throw new NullPointerException();
//
//        return product;
//    }

//    @ExceptionHandler(Exception.class)
//    public ModelAndView error(Exception ex){
//        ModelAndView mv = new ModelAndView("error");
//
//        mv.addObject("msg",ex.getMessage());
////        mv.setViewName("error");
//        return mv;
//    }

}
